% function analyCascadeFineTuning
%% this shows the mean PSNR,and iteration
% clc; clear;
% close all;
data_path = srpath.getDataPath;
result_path = srpath.getResultPath('CascadeFineTuning2');

scale = 3;
interval = 100;

%%
% ImgPath = fullfile(data_path, 'Set14');
ImgPath = fullfile(data_path, 'Set5');
%  ImgPath = fullfile(data_path, 'mytest');
% ImgPath = fullfile(data_path, 'General100');
% ImgPath = fullfile(data_path, 'B100');

%%
ImgCell = srimg.genLHPairs(ImgPath, scale);
ImgCell = ImgCell(:, 2:3);
ImgCell0 = ImgCell;

%%
% % net.folders = {'VDSR_y-1', '[VDSR_y-1]-tuning2-[VDSR_y-1]', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-1]'};
% % iters = {100};
% 
% % net.folders = {'VDSR_y-1', '[VDSR_y-2]-tuning2-[VDSR_y-1]', '[[VDSR_y-2]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-1]'};
% % iters = [
% %     500 100 100
% %     500 500 500
% %     ];
% 
% net.folders = {'VDSR_y-1', '[VDSR_y-1]-snapshot2-[VDSR_y-1]', '[VDSR_y-1]-tuning2-[VDSR_y-1]', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-1]'};
% iters = [
%     100 600 100 100
%     500 1500 500 500
%     ];
% 
% mynet.name = 'VDSR_y';
% net.deploy_file = [mynet.name '_deploy.prototxt'];
% mynet.model_prefix = [mynet.name '_iter_'];
% 
% psnrList = cell(1, length(net.folders));
% for i = 1 : length(net.folders)
%     mynet.model_path = fullfile(result_path, net.folders{i}, 'models');
%     mynet.deploy_file = fullfile(result_path, net.folders{i}, net.deploy_file);
%     
%     if i ~= 2
%         [psnrList{i}, ImgCell] = srimg.analyPSNR2(ImgCell, scale, mynet, [iters(1, i), interval, iters(2, i)]);
%     else
%         psnrList{i} = srimg.analyPSNR2(ImgCell0, scale, mynet, [iters(1, i), interval, iters(2, i)]);
%     end
% end

%%
% net.folders = {'VDSR_y-1', '[VDSR_y-1]-tuning2-[VDSR_y-1]', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-1]', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-2]', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-3]'};
% iters = [
%     500 500 100 100 100
%     500 500 500 500 500
%     ];

%%
net.folders = {'VDSR_y-1', 'VDSR_y-1', 'VDSR_y-1', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-2]', '[[VDSR_y-1]-tuning2-[VDSR_y-1]]-tuning2-[VDSR_y-3]'};
iters = [
    500 500 500 100 100
    500 500 500 500 500
    ];

mynet.name = 'VDSR_y';
net.deploy_file = [mynet.name '_deploy.prototxt'];
mynet.model_prefix = [mynet.name '_iter_'];

psnrList = cell(1, length(net.folders));
for i = 1 : 2
    mynet.model_path = fullfile(result_path, net.folders{i}, 'models');
    mynet.deploy_file = fullfile(result_path, net.folders{i}, net.deploy_file);
    
    [psnrList{i}, ImgCell] = srimg.analyPSNR2(ImgCell, scale, mynet, [iters(1, i), interval, iters(2, i)]);
end

for i = 3 : length(net.folders)
    mynet.model_path = fullfile(result_path, net.folders{i}, 'models');
    mynet.deploy_file = fullfile(result_path, net.folders{i}, net.deploy_file);
    
    psnrList{i} = srimg.analyPSNR2(ImgCell, scale, mynet, [iters(1, i), interval, iters(2, i)]);
end

%%
myinput.save(psnrList);

meanpsnrList = cellfun(@mean, psnrList, 'uniformoutput', false);
for i = 1 : length(meanpsnrList)
    fprintf('%d  ', iters(1, i) : interval : iters(2, i));
    fprintf('\n')
    fprintf('%.2f ', meanpsnrList{i});
    fprintf('\n======================\n')
end

%%
% lens = cellfun(@(x)size(x, 1), psnrList);
% psnr_bar = mean(psnrList);

%% Finally,we draw the relationship between iteration(caffemodel) and psnr
%figure;plot(psnr_iter_srcnn-clip,'r');hold on;plot(psnr_iter_bic,'--g');hold on;plot(psnr_iter_goal,'b');xlabel('srcnn-clip-iteration');ylabel('srcnn-clip-psnr');title('this my srcnn-clip');legend('psnr-iter-srcnn-clip','psnr-iter-bic','psnr-iter-goal');

% figure;
% pp = 0;
% for i = 1 : length(psnrList)-1
%     pp = pp(end) + 1 : pp(end) + size(psnrList{i}, 2);
%     plot(pp, mean(psnrList{i}), '--s'); hold on;
% end
% plot(mean(psnrList{i+1}), '--s');
% axis tight
% xlabel('Iteration'); ylabel('PSNR');

%%
% figure;
% pp = 0;
% for i = 1 : length(psnrList)
%     switch i
%         case 1
%             pp(end) = 0;
%         case 2
%             pp(end) = 4;
%     end
%     pp = pp(end) + 1 : pp(end) + size(psnrList{i}, 2);
%     plot(pp, mean(psnrList{i}), '--s'); hold on;
% end
% 
% axis tight
% xlabel('Iteration'); ylabel('PSNR');
